package com.pearadmin.pro.modules.stu.service.impl;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pearadmin.pro.common.web.base.page.PageResponse;
import com.pearadmin.pro.common.web.base.page.Pageable;

import com.pearadmin.pro.modules.stu.domain.*;
import com.pearadmin.pro.modules.stu.dto.ManageDto;
import com.pearadmin.pro.modules.stu.dto.OrderListDto;
import com.pearadmin.pro.modules.stu.param.OrderVo;
import com.pearadmin.pro.modules.stu.param.StuManageVo;
import com.pearadmin.pro.modules.stu.param.StuOrderRequest;
import com.pearadmin.pro.modules.stu.repository.*;
import com.pearadmin.pro.modules.stu.service.StuOrderCourseService;
import com.pearadmin.pro.modules.stu.service.StuOrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;


/**
 * 学生订单Service业务层处理
 *
 * @author chenJian
 * @date 2021-09-09
 */
@Service
public class StuOrderServiceImpl extends ServiceImpl<StuOrderRepository, StuOrder> implements StuOrderService {
    @Resource
    private StuOrderRepository stuOrderRepository;
    @Autowired
    private StuOrderService stuOrderService;
    @Autowired
    private StuOrderCourseService stuOrderCourseService;
    @Autowired
    private StuRollRepository stuRollRepository;
    @Autowired
    private StuGradeRepository stuGradeRepository;
    @Autowired
    private StuTranscriptAddRepository stuTranscriptAddRepository;
    @Autowired
    private StuOrderDepositRepository stuOrderDepositRepository;
    @Autowired
    private  StuBuspRepository  stuBuspRepository;
    @Autowired
    private   StuOrderCourseRepository  stuOrderCourseRepository;

    @Override
    public boolean addNewOrder(OrderVo param) {
        if (StringUtils.isBlank(param.getOdrCurInfo())) {
            return false;
        }
        StuOrder order = new StuOrder();
        order.setBuspId(param.getBuspId());
        order.setOrderSn(IdUtil.getSnowflake(1, 2).nextIdStr());
        order.setOrderType("1");
        order.setPrePolicy(param.getPrePolicy());
        order.setTotalAmt(param.getTotalAmt());
        order.setPreAmt(param.getPreAmt());
        order.setRecAmt(param.getRecAmt());
        order.setIsSpecial(param.getIsSpecial());
        order.setSpecialAmt(param.getSpecialAmt());
        order.setSpecialRemark(param.getSpecialRemark());
        if (1L == param.getIsSpecial()) {
            order.setSpecialCheckStatus(0L);
        } else {
            order.setSpecialCheckStatus(1L);
        }
        if (null != param.getDepositId()) {//修改定金状态
            order.setDepositId(param.getDepositId());
            StuOrderDeposit deposit = stuOrderDepositRepository.selectById(param.getDepositId());
            deposit.setUpdateTime(LocalDateTime.now());
            deposit.setDeductionStatus(1L);
            stuOrderDepositRepository.updateById(deposit);
        }
        order.setPayChn(param.getPayChn());
        order.setPayStatus(0L);
        order.setAgreStatus(0L);
        order.setAgreSend(0L);
        order.setCreateTime(LocalDateTime.now());
        stuOrderService.saveOrUpdate(order);
        if (StringUtils.isNotBlank(param.getOdrCurInfo())) {//商机意向课程信息
            JSONArray jsonArray = (JSONArray) JSONArray.parse(param.getOdrCurInfo());
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsb = (JSONObject) jsonArray.get(i);
                StuOrderCourse course = new StuOrderCourse();
                course.setOrderId(order.getId());
                course.setSemester(jsb.get("semester").toString());
                course.setSchool(jsb.get("school").toString());
                course.setYuarDate(jsb.get("yuarDate").toString());
                course.setQuarter(jsb.get("quarter").toString());
                course.setProject(jsb.get("project").toString());
                course.setCozName(jsb.get("cozName").toString());
                course.setCozNum(jsb.get("cozNum").toString());
                course.setBeginTime(Convert.toDate(jsb.get("beginTime")));
                course.setEndTime(Convert.toDate(jsb.get("endTime")));
                course.setPrice(Convert.toBigDecimal(jsb.get("price")));
                course.setCreateTime(LocalDateTime.now());
                stuOrderCourseService.saveOrUpdate(course);
            }
        }
        return true;
    }


    /**
     * 查询学生订单
     *
     * @param request
     * @return 学生订单 分页集合
     */
    @Override
    public PageResponse<OrderListDto> page(StuOrderRequest request) {
        return Pageable.of(request, (() -> stuOrderRepository.selOrders(request)));
    }

    /**
     * 查询学生订单
     *
     * @param id 学生订单ID
     * @return 学生订单
     */

    @Override
    public StuOrder selectStuOrderById(Long id) {
        StuOrder order = stuOrderRepository.selectById(id);
        //学生基本信息
        StuBusp stuBusp = stuBuspRepository.selectById(order.getBuspId());
        order.setBusp(stuBusp);
        //学籍信息
        LambdaQueryWrapper<StuRoll> rollLambdaQueryWrapper = new LambdaQueryWrapper<>();
        rollLambdaQueryWrapper.eq(StuRoll::getBuspId, order.getBuspId());
        List<StuRoll> stuRolls = stuRollRepository.selectList(rollLambdaQueryWrapper);
        order.setRolls(stuRolls);
        //学生成绩单信息
        LambdaQueryWrapper<StuGrade> gradeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        gradeLambdaQueryWrapper.eq(StuGrade::getBuspId, order.getBuspId());
        List<StuGrade> grades = stuGradeRepository.selectList(gradeLambdaQueryWrapper);
        order.setGrades(grades);
        //成绩单地址信息
        LambdaQueryWrapper<StuTranscriptAdd> transcriptAddLambdaQueryWrapper = new LambdaQueryWrapper<>();
        transcriptAddLambdaQueryWrapper.eq(StuTranscriptAdd::getBuspId, order.getBuspId());
        List<StuTranscriptAdd> transcriptAdds = stuTranscriptAddRepository.selectList(transcriptAddLambdaQueryWrapper);
        order.setTranscriptAdds(transcriptAdds);
        //订单课程信息
        LambdaQueryWrapper<StuOrderCourse> orderCourseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        orderCourseLambdaQueryWrapper.eq(StuOrderCourse::getOrderId, order.getId());
        List<StuOrderCourse> stuOrderCourses = stuOrderCourseRepository.selectList(orderCourseLambdaQueryWrapper);
        order.setOrderCourses(stuOrderCourses);

        return order;
    }

    @Override
    public PageResponse<OrderListDto> managePage(StuManageVo vo) {
        return  Pageable.of(vo, (() -> stuOrderRepository.selStuManageOrders(vo)));
    }


}
